//
//  RankListDetailViewController.m
//  LuckyRadio
//
//  Created by Mr、J on 15/11/21.
//  Copyright © 2015年 chenjun. All rights reserved.
//

#import "RankListDetailViewController.h"
#import "CustomerNetworking.h"
#import "RankDetailModel.h"
#import <UIImageView+WebCache.h>
#import "SongsModel.h"
#import "RankListDetailTableViewCell.h"
#import "LGCLastViewController.h"
#import "SongList.h"
#import <MJRefresh.h>
#import <MBProgressHUD.h>

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define WIDTH_PRO(X) (X)*(SCREEN_WIDTH/375)

#define HEIGHT_PRO(Y) (Y)*(SCREEN_HEIGHT/667)


@interface RankListDetailViewController ()<UITableViewDataSource, UITableViewDelegate, MBProgressHUDDelegate>

@property (nonatomic, retain) UIImageView *headImage;

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) UILabel *titleLabel;

@property (nonatomic, retain) UIImageView *tagImageView;

@property (nonatomic, retain) UILabel *nameLabel;

@property (nonatomic, retain) UILabel *numLabel;

@property (nonatomic, retain) UIButton *playAllBtn;

@property (nonatomic, retain) UIImageView *playImage;

@property (nonatomic, retain) UILabel *playLabel;

@property (nonatomic, retain) UILabel *tagLabel;

@property (nonatomic, retain) NSMutableArray *songsArray;

@property (nonatomic, retain) UIView *totalView;

@property (nonatomic, retain) MBProgressHUD *HUD;

@property (nonatomic, retain) UIView *zhanView;

@end

@implementation RankListDetailViewController
- (void)dealloc
{
    [_zhanView release];
    [_titleLabel release];
    [_tagImageView release];
    [_nameLabel release];
    [_numLabel release];
    [_playAllBtn release];
    [_playImage release];
    [_playLabel release];
    [_tagLabel release];
    [_songsArray release];
    [_headImage release];
    [_tableView release];
    [_totalView release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSLog(@"🍎%@", _rankID);
    self.view.backgroundColor = [UIColor whiteColor];
    self.songsArray = [NSMutableArray array];
     [self createSubViews];
    [self loadData];
}

-(void)createSubViews
{
    
    
    self.totalView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT_PRO(300))];
    [self.view addSubview:self.totalView];
    [_totalView release];
    
    self.headImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0 , SCREEN_WIDTH, HEIGHT_PRO(250))];
    [self.totalView addSubview:self.headImage];
    [_headImage release];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH_PRO(10), self.headImage.frame.size.height - HEIGHT_PRO(50), SCREEN_WIDTH - WIDTH_PRO(20), HEIGHT_PRO(20))];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.font = [UIFont systemFontOfSize:20 weight:2];
    [self.headImage addSubview:self.titleLabel];
    [_titleLabel release];
    
    self.tagImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH_PRO(10), self.titleLabel.frame.origin.y + self.titleLabel.frame.size.height + HEIGHT_PRO(10), WIDTH_PRO(20), HEIGHT_PRO(20))];
    self.tagImageView.image = [UIImage imageNamed:@"iconfont-headphone"];
    [self.headImage addSubview:self.tagImageView];
    [_tagImageView release];
    
    self.numLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.tagImageView.frame.origin.x + self.tagImageView.frame.size.width + WIDTH_PRO(10), self.tagImageView.frame.origin.y, WIDTH_PRO(60), HEIGHT_PRO(20))];
    self.numLabel.textColor = [UIColor whiteColor];
    self.numLabel.font = [UIFont systemFontOfSize:12];
    [self.headImage addSubview:self.numLabel];
    [_numLabel release];
    
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.numLabel.frame.origin.x + self.numLabel.frame.size.width + WIDTH_PRO(20), self.numLabel.frame.origin.y, SCREEN_WIDTH - self.tagImageView.frame.size.width - self.numLabel.frame.size.width - WIDTH_PRO(40), HEIGHT_PRO(20))];
    self.nameLabel.textColor = [UIColor whiteColor];
    self.nameLabel.font = [UIFont systemFontOfSize:12];
    [self.headImage addSubview:self.nameLabel];
    [_nameLabel release];
    
    self.playAllBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.playAllBtn.frame = CGRectMake(0, self.totalView.frame.size.height - HEIGHT_PRO(50) , SCREEN_WIDTH / 2, HEIGHT_PRO(50));
    [self.playAllBtn addTarget:self action:@selector(pushArrayData) forControlEvents:UIControlEventTouchUpInside];
    [self.totalView addSubview:self.playAllBtn];
    
    self.playImage = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH_PRO(20), HEIGHT_PRO(10), WIDTH_PRO(40), HEIGHT_PRO(40))];
    self.playImage.image = [UIImage imageNamed:@"Recognize_Play"];
    [self.playAllBtn addSubview:self.playImage];
    [_playImage release];
    
    self.playLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.playImage.frame.origin.x + self.playImage.frame.size.width + WIDTH_PRO(10), HEIGHT_PRO(15), WIDTH_PRO(100), HEIGHT_PRO(20))];
    self.playLabel.font = [UIFont systemFontOfSize:15];
    self.playLabel.text = @"播放全部";
    [self.playAllBtn addSubview:self.playLabel];
    [_playLabel release];
    
    self.tagLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.playLabel.frame.origin.x, self.playLabel.frame.origin.y + self.playLabel.frame.size.height , self.playLabel.frame.size.width, HEIGHT_PRO(20))];
    self.tagLabel.textColor = [UIColor grayColor];
    self.tagLabel.font = [UIFont systemFontOfSize:12];
    [self.playAllBtn addSubview:self.tagLabel];
    [_tagLabel release];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - HEIGHT_PRO(64)) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = self.totalView;
    [_tableView release];
    
    self.zhanView = [[UIView alloc] initWithFrame:self.view.bounds];
    self.zhanView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.zhanView];
    [_zhanView release];

    
}

-(void)loadData
{
    self.HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
    self.HUD.delegate = self;
    
    //常用的设置
    //小矩形的背景色
    // self.HUD.color = [UIColor whiteColor];//这儿表示无背景
    //显示的文字
    self.HUD.labelText = @"正在进行加载~";
    
    [CustomerNetworking customerWithURL:[NSString stringWithFormat:@"http://api.songlist.ttpod.com/ranklists/%@/current?app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5S&f=f234&s=s310&imsi=&hid=&splus=9.1&active=1&net=2&openudid=e97c1f17f916efa173065a10152c7506e65b60f7&idfa=5FBD2BF5-1484-4C9B-A138-4D9CE22EADB5&utdid=VkKezJUW5SwDAGga0yzrZnyO&alf=700606&bundle_id=com.ttpod.music&latitude=38.89679246042943&longtitude=121.5286685343515", _rankID] andParamer:nil andBlock:^(id response) {
        
        RankDetailModel *rankDetailModel = [RankDetailModel modelWithDictionary:response];
       
        for (NSDictionary *dic in rankDetailModel.songs) {
            SongsModel *songsModel = [SongsModel modelWithDictionary:dic];
            [self.songsArray addObject:songsModel];
            NSLog(@"123456%ld", _songsArray.count);

            
        }
        
        [self.headImage sd_setImageWithURL:[NSURL URLWithString:[rankDetailModel.image objectForKey:@"pic"]]];
        self.tagLabel.text = [NSString stringWithFormat:@"%ld首", rankDetailModel.songs.count];
        self.titleLabel.text = rankDetailModel.title;
        self.numLabel.text = [NSString stringWithFormat:@"%@", rankDetailModel.listen_count];
        if ([rankDetailModel.listen_count floatValue] > 10000) {
            CGFloat count = [rankDetailModel.listen_count floatValue] / 10000;
            self.numLabel.text = [NSString stringWithFormat:@"%.1f万", count];
        }
        self.nameLabel.text = rankDetailModel.desc;

        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.zhanView removeFromSuperview];
        [self.HUD hide:YES];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"@#$%ld", _songsArray.count);
    return _songsArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HEIGHT_PRO(60);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SongsModel *songsModel = _songsArray[indexPath.row];
    RankListDetailTableViewCell *cell = [RankListDetailTableViewCell rankListDetailTableViewCellWith:tableView];
    cell.numLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    cell.songsModel = songsModel;
    return cell;

    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SongsModel *songModel = [_songsArray objectAtIndex:indexPath.row];
    LGCLastViewController *lgcLastView = [LGCLastViewController shareLastViewController];
    lgcLastView.indexRow = indexPath.row;
    if (songModel.auditionList.count != 0) {
        if (songModel.auditionList.count == 3){
            lgcLastView.playUrl = [songModel.auditionList[2] objectForKey:@"url"];
            lgcLastView.strTitle = songModel.name;
        }else if (songModel.auditionList.count == 2){
            lgcLastView.playUrl = [songModel.auditionList[1] objectForKey:@"url"];
            lgcLastView.strTitle = songModel.name;
        }else {
            lgcLastView.playUrl = [songModel.auditionList[0] objectForKey:@"url"];
            lgcLastView.strTitle = songModel.name;
        }
    }
    NSMutableArray *array = [NSMutableArray array];
    for (SongsModel *songsModel in _songsArray) {
        SongList *songList = [[SongList alloc] init];
        if (songsModel.auditionList.count != 0) {
            if (songsModel.auditionList.count == 3){
                songList.song_play_url = [songsModel.auditionList[2] objectForKey:@"url"];
                songList.song_name = songsModel.name;
                [array addObject:songList];
            }else if (songsModel.auditionList.count == 2){
                songList.song_play_url = [songsModel.auditionList[1] objectForKey:@"url"];
                songList.song_name = songsModel.name;
                [array addObject:songList];
            }else {
                songList.song_play_url = [songsModel.auditionList[0] objectForKey:@"url"];
                songList.song_name = songsModel.name;
                [array addObject:songList];
            }
        }
    }
    lgcLastView.array_current = array;
    
    
    [self.navigationController presentViewController:lgcLastView animated:YES completion:nil];
}

-(void)pushArrayData
{
    SongsModel *songModel =_songsArray[0];
    
    LGCLastViewController *lastView = [LGCLastViewController shareLastViewController];
    lastView.tempArray = _songsArray;
    if (songModel.auditionList.count != 0) {
        if (songModel.auditionList.count == 3){
            lastView.playUrl = [songModel.auditionList[2] objectForKey:@"url"];
            lastView.strTitle = songModel.name;
        }else if (songModel.auditionList.count == 2){
            lastView.playUrl = [songModel.auditionList[1] objectForKey:@"url"];
            lastView.strTitle = songModel.name;
        }else {
            lastView.playUrl = [songModel.auditionList[0] objectForKey:@"url"];
            lastView.strTitle = songModel.name;
        }
    }
    NSMutableArray *array = [NSMutableArray array];
    for (SongsModel *songsModel in _songsArray) {
        SongList *songList = [[SongList alloc] init];
        if (songsModel.auditionList.count != 0) {
            if (songsModel.auditionList.count == 3){
                songList.song_play_url = [songsModel.auditionList[2] objectForKey:@"url"];
                songList.song_name = songsModel.name;
                [array addObject:songList];
            }else if (songsModel.auditionList.count == 2){
                songList.song_play_url = [songsModel.auditionList[1] objectForKey:@"url"];
                songList.song_name = songsModel.name;
                [array addObject:songList];
            }else {
                songList.song_play_url = [songsModel.auditionList[0] objectForKey:@"url"];
                songList.song_name = songsModel.name;
                [array addObject:songList];
            }
        }
    }
    lastView.array_current = array;

    [self.navigationController presentViewController:lastView animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
